package com.gticket.customerbackend.dlx;

import com.gticket.customerbackend.util.RabbitUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 李浩楠
 * @date 2019/11/25 下午4:20
 */
public class DlxSender {
    private final static String exchangeName = "direct-exchange-normal-customerbackend-schedulemovie";
    private static final Logger log = LoggerFactory.getLogger(DlxSender.class);


    public static void sendMessage(String message,long times) throws Exception {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        //创建用作死信的交换机和队列
        channel.exchangeDeclare("dlx_exchange_schedule", BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare("dlx_queue1_schedule", true, false, false, null);
        channel.queueBind("dlx_queue1_schedule", "dlx_exchange_schedule", "schedule");


        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 604800000);//看时间
        arguments.put("x-dead-letter-exchange", "dlx_exchange_schedule");


        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        builder.deliveryMode(2);
        builder.expiration(String.valueOf(times));
        AMQP.BasicProperties properties = builder.build();

        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare("schedule_queue", true, false, false,arguments);
        channel.queueBind("schedule_queue", exchangeName, "schedule");


        channel.basicPublish(exchangeName, "schedule", properties, message.getBytes());
        channel.close();
        connection.close();
    }

    public static void sendMessage2(String message,long end_times) throws Exception {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        //创建用作死信的交换机和队列
        channel.exchangeDeclare("dlx_exchange_schedule", BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare("dlx_queue1_schedule_end", true, false, false, null);
        channel.queueBind("dlx_queue1_schedule_end", "dlx_exchange_schedule", "schedule_end");


        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 604800000);//看时间
        arguments.put("x-dead-letter-exchange", "dlx_exchange_schedule");


        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        builder.deliveryMode(2);
        builder.expiration(String.valueOf(end_times));
        AMQP.BasicProperties properties = builder.build();

        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare("schedule_queue_end", true, false, false,arguments);
        channel.queueBind("schedule_queue_end", exchangeName, "schedule_end");


        channel.basicPublish(exchangeName, "schedule_end", properties, message.getBytes());
        channel.close();
        connection.close();
    }
}
